####################################################################
# C Figures 1 and 2 in the main text and Figures A2 and A3 
# in the supporting appendix
# title: External Threat Environments and Individual Bias against Female Leaders
# author:	Nam Kyu Kim and Alice Kang											 	
####################################################################

library(foreign)
library(dplyr)
library(reshape2)
library(ggplot2)
library(readstata13)
library(gridExtra)
library(countrycode)

option= theme(
  plot.title = element_text(size=12,vjust=1, hjust = 0.5, face = "bold"),
  axis.title.y = element_text(size = 12, vjust=1),
  axis.title.x = element_text(size = 12, vjust=0),
  axis.text.x=element_text(size=11),
  axis.text.y=element_text(size=10),
  strip.text.x = element_text(size = 12),
  panel.grid.minor = element_line(colour = NA),
  panel.grid.major.x = element_blank(), 
  panel.grid.major.y = element_blank(), 
  panel.border = element_blank(),
  axis.line=element_line(),
  axis.line.y=element_blank(),
  axis.line.x = element_line(color="black", size = .5), 
  legend.title=element_blank(),
  strip.text= element_text(face = "bold"),       
  strip.background = element_rect(colour = NA, fill = NA)
)


option2 = theme(
  plot.title = element_text(size=13,vjust=1, hjust = 0.5, face = "bold"),
  axis.title.y = element_text(size = 12,vjust=1),
  axis.title.x = element_text(size = 12),
  axis.text.x=element_text(size=12),
  axis.text.y=element_text(size=11),
  strip.text.x = element_text(size = 12),
  panel.grid.minor = element_line(colour = NA),
  panel.grid.major=element_line(linetype="dotted", size=1),
  panel.grid.major.x = element_blank(), 
  panel.border = element_blank(),
  axis.line.x = element_line(color="black", size = .5), 
  axis.line.y = element_line(color="black", size = .5),
  axis.line=element_line(),
  legend.title=element_blank(),
  strip.text= element_text(face = "bold"),       
  strip.background=element_rect(colour="#f0f0f0",fill="#f0f0f0")
  )

## Figure 1
data <-read.dta13("MainData.dta")

data %>% 
  filter(!is.na(d059_2)) %>% 
  group_by(country) %>%
  summarize(mean= mean(d059_2)-1) %>%
  ggplot(aes(x = reorder(country, mean), y = mean)) +
  theme_bw() + 
  coord_flip()+
  geom_bar(stat = "identity", alpha=.3, width = .4)+
  scale_y_continuous(limits=c(0,3), breaks = seq(0, 3, by =1))+
  ylab("Agreement that men make better leaders than women") + 
  xlab("") + 
  option

ggsave(file="average.pdf", width=11, height=16)

## Figure 2

## both post1.dta and post2.dta obtained from running Stata codes for Table 4

t1 <-read.dta13("post1.dta")
t1$z <- c(0, 1)
t1$parm[t1$parm=="0.sex"] <- "Male"
t1$parm[t1$parm=="1.sex"] <- "Female"


t2 <-read.dta13("post2.dta")
t2 <- t2[12:22,]
t2$z <- seq(0, 1, by=.1)

g1=
  ggplot(t1, aes(x=parm, y=estimate))+
  theme_bw() + 
  geom_line(size=.8) + 
  geom_pointrange(aes(x = parm, ymin=estimate-stderr*1.96, ymax=estimate+stderr*1.96))+
  ggtitle("Marginal effects of External threat")+
  ylab("Marginal effects")+ 
  xlab("")+
  option2

g2=
  ggplot(t2, aes(x=z, y=estimate))+
  theme_bw() + 
  geom_line(size=.8) + 
  geom_ribbon(aes(x =z, ymin=estimate-stderr*1.96, ymax=estimate+stderr*1.96), alpha=0.2, fill="darkgray")+
  ggtitle("Marginal effects of Female")+
  ylab("Marginal effects")+ 
  xlab("External threats")+
  scale_x_continuous(breaks = seq(0, 1, by =.2))+
  option2

grid.arrange(g1,g2, ncol=2)
g <- arrangeGrob(g1, g2, nrow=1)
ggsave(g, file="margins.pdf", width=10, height=5)


## Figure A2

data <-read.dta13("threat.dta")

codes.of.origin <- data$cowcode
country <- countrycode(codes.of.origin,"cown","country.name")
data$country <- country
data$country[data$cowcode == 345] <- "Serbia"

data2 <- data %>%
  filter(year >= 1990) %>%
  group_by(country, wvs_sample) %>%
  summarize(mean = mean(threat_cinc_w)) %>%
  mutate(above = ifelse(mean > .55037, 1, 0)) 

ggplot(data2[data2$above == 1,], aes(x = reorder(country, mean), y = mean)) +
  theme_bw() + 
  coord_flip()+
  geom_bar(aes(fill = factor(wvs_sample)), stat = "identity", alpha=.8, width = .5)+
  scale_fill_discrete(labels=c("WVS=0","WVS=1"))+
  ylab("External threat scale") + 
  xlab("") + 
  option

ggsave(file="ivaverage1.pdf", width=12, height=15)

ggplot(data2[data2$above == 0,], aes(x = reorder(country, mean), y = mean)) +
  theme_bw() + 
  coord_flip()+
  geom_bar(aes(fill = factor(wvs_sample)), stat = "identity", alpha=.8, width = .5)+
  scale_y_continuous(limits = c(0, 1.6))+
  scale_fill_discrete(labels=c("WVS=0","WVS=1"))+
  ylab("External threat scale") + 
  xlab("") + 
  option

ggsave(file="ivaverage2.pdf", width=12, height=15)

## Figure A3

data %>%
  group_by(year) %>%
  summarize(pmean = mean(threat_cinc_w, na.rm = TRUE)) %>%
  ggplot(aes(y=pmean, x=year)) + 
  theme_bw() + 
  geom_line(colour="black") +
  scale_x_continuous(breaks = seq(1950, 2012, 10)) +
  ylim(c(0, .85))+
  ylab("External threat scale") + 
  xlab("Year") + 
  option2

ggsave(file="trend.pdf", width=7, height=6)


